﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.HVAC;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class HYPSST : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			UIApplication application = commandData.Application;
			Autodesk.Revit.ApplicationServices.Application application2 = application.Application;
			Document document = application.ActiveUIDocument.Document;
			Selection selection = commandData.Application.ActiveUIDocument.Selection;
			HYPipeConnect hypipeConnect = new HYPipeConnect();
			ISelectionFilter selectionFilter = new RiserPipeFilter();
			transaction = new Transaction(document, "四通连接");
			transaction.Start();
			Result result;
			try
			{
				Reference reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, selectionFilter, "请选择一根立管");
				Pipe piping = document.GetElement(reference) as Pipe;
				reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, "请选择支管1……");
				Pipe pipe = document.GetElement(reference) as Pipe;
				reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, "请选择支管2……");
				Pipe pipe2 = document.GetElement(reference) as Pipe;
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(piping);
				YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipe);
				YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(pipe2);
				XYZ xyz = hypiping.IntersectionPoint(hypiping2.Piping, 0.02);
				XYZ xyz2 = hypiping.IntersectionPoint(hypiping3.Piping, 0.02);
				if (Math.Abs(xyz.X - xyz2.X) > 0.02 || Math.Abs(xyz.Y - xyz2.Y) > 0.02 || Math.Abs(xyz.Z - xyz2.Z) > 0.02)
				{
					YJKMessageBox.Information("选择的立管和两个支管没有相同的交点,请调整两个支管的位置");
					result = Autodesk.Revit.UI.Result.Failed;
				}
				else
				{
					List<Pipe> list = new List<Pipe>();
					list = new YJKPiping<Pipe>(piping).Separate(xyz);
					Pipe item;
					Pipe item2;
					if (this.getMosthighPoint(list[0]).Z > this.getMosthighPoint(list[1]).Z)
					{
						item = list[0];
						item2 = list[1];
					}
					else
					{
						item = list[1];
						item2 = list[0];
					}
					List<Pipe> list2 = new List<Pipe>();
					list2.Add(item);
					list2.Add(item2);
					list2.Add(pipe);
					list2.Add(pipe2);
					FamilySymbol familySymbol = this.GetFamilySymbolByName("立体顺水四通", document);
					if (familySymbol == null)
					{
						familySymbol = new OperateSegmentFitting(document).LoadFamilySymbolByName("立体顺水四通", HYFamilySymbolSign.Cross);
					}
					hypipeConnect.NewDownCrossFitting(list2, familySymbol);
					transaction.Commit();
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				YJKMessageBox.Information(ex.Message);
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null && (int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
			}
			return result;
		}

		private XYZ getMosthighPoint(Pipe pipe)
		{
			if ((pipe.Location as LocationCurve).Curve.GetEndPoint(0).Z <= (pipe.Location as LocationCurve).Curve.GetEndPoint(1).Z)
			{
				return (pipe.Location as LocationCurve).Curve.GetEndPoint(1);
			}
			return (pipe.Location as LocationCurve).Curve.GetEndPoint(0);
		}

		private FamilySymbol GetFamilySymbolByName(string familyName, Autodesk.Revit.DB.Document doc)
		{
			FamilySymbol result = null;
			List<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(FamilySymbol), doc);
			if (elementByClass.Count <= 0)
			{
				return null;
			}
			foreach (Element element in elementByClass)
			{
				FamilySymbol familySymbol = element as FamilySymbol;
				if (familySymbol != null && familySymbol.Family.Name == "HY_" + familyName)
				{
					result = familySymbol;
					break;
				}
			}
			return result;
		}
	}
}
